package com.wuxiaofan.secondkill.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.common.vo.ReturnResult;
import com.wuxiaofan.secondkill.entity.User;
import com.wuxiaofan.secondkill.entity.custom.UserCustom;
import com.wuxiaofan.secondkill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tUser")
public class UserApi {


    @Autowired
    private UserService tUserService;

    @RequestMapping("findById")
    public ReturnResult findById(Long id) {
        User tUser = tUserService.findById(id);
        return ReturnResult.success("", tUser);
    }


    @RequestMapping("findByMap")
    public ReturnResult findByMap(@RequestBody JSONObject jsonObject) {
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        List<UserCustom> tUserCustoms = tUserService.findByMap(paraMap);
        return ReturnResult.success("", tUserCustoms);
    }

    @RequestMapping("findByPage")
    public ReturnResult findByPage(@RequestBody JSONObject jsonObject) {
        Page page = new Page(jsonObject.getIntValue("current"), jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<UserCustom> tUserCustoms = tUserService.findByMap(paraMap, page);
        return ReturnResult.success("", tUserCustoms);
    }

    @RequestMapping("save")
    public ReturnResult save(@RequestBody User tUser) {
        tUser = tUserService.save(tUser);
        return ReturnResult.success("保存成功", tUser);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject) {
        Map paraMap = jsonObject;
        tUserService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }


    @RequestMapping("delById")
    public ReturnResult delById(@RequestBody JSONObject jsonObject) {
        tUserService.delById(jsonObject.getString("id"));
        return ReturnResult.success("删除成功");
    }


}
